home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / WAIS / ir / macbuild.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  7.5 KB  |  283 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    Brewster@think.com
  6. */
  7.  
  8. /* This is a clumsy index building program for the Mac.
  9.  * For some reason all this Class garbage is needed to
  10.  * set up something to call the file routines.
  11.  * Sorry about the novice mac code.
  12.  *
  13.  * -brewster 7/90
  14.  */
  15.  
  16. #include "irfiles.h"
  17. #include "irhash.h"
  18. #include "irtfiles.h"
  19. #include "panic.h"
  20. #include "cutil.h"
  21. #include "irext.h"
  22.  
  23. #include <profile.h>
  24. #include <EventMgr.h>
  25. #include <FileMgr.h>
  26.  
  27. /*----------------------------------------------------------------------*/
  28. /* Class definitions */
  29.  
  30. #include <CApplication.h>
  31. #include <CFile.h>
  32. #include <CView.h>
  33. #include <CWindow.h>
  34. #include <CDesktop.h>
  35. #include <CDirector.h>
  36. #include "CDynamicError.h"
  37.  
  38. extern    CApplication    *gApplication;
  39.  
  40. struct CBuildApp : CApplication 
  41. {
  42.   void    IBuildApp(void);
  43. };
  44.  
  45. void CBuildApp::IBuildApp(void)
  46. {
  47.   CApplication::IApplication(4, 20480L, 2048L);
  48.   gError->Dispose(); /* get rid if the CError that IApplication defined */
  49.   gError = new(CDynamicError); /* make our own error handler */
  50. }
  51.  
  52. /*----------------------------------------------------------------------*/
  53. /* Gets a full filename from the mac file objects.
  54.  * getwd was taken from utils.c from the 
  55.  * document retrieval system.
  56.  */
  57.  
  58. #include <HFS.H>
  59. #include <string.h>
  60. /* for file manipulation routines */
  61. typedef enum file_style {MAC_FILE_STYLE, UNIX_FILE_STYLE} file_style;
  62.  
  63. static char 
  64. *getwd(file_style style)
  65. /* This function returns the unix style path name which is set by any of the SF
  66.    routines (using the global vars).  The code is from the net, I have no idea
  67.    who the author might be.  He/She does include the following note:
  68.    
  69.           Note that it is perfectly legal for a Macintosh owner to create a
  70.         directory hierarchy where the length of full path names of the deepest
  71.         files exceeds 255 bytes; since the file system never manipulates full
  72.         pathnames internally (it only ever sees them when passed as parameters),
  73.         it doesn't and needn't check.  However, this poses an ethical problem if
  74.         you are constructing full pathnames: my code simply bombs if it would
  75.         construct a pathname >255 bytes, and if I increased the buffer size, the
  76.         resulting pathnames are useless except for documentation purposes (since
  77.         the file system can't have string parameters >255 bytes).
  78.  */
  79. {
  80.     CInfoPBRec d;
  81.     static char ret[255];
  82.     char nm[50], tmp[255];
  83.     long cur_dir = CurDirStore; /* mac global var */
  84.     long cur_vol = 0 - SFSaveDisk; /* mac global var */
  85.     
  86.     ret[0] = '\0';
  87.     d.dirInfo.ioDrDirID = cur_dir;
  88.     for(;;) {
  89.         d.dirInfo.ioCompletion = 0;
  90.         d.dirInfo.ioNamePtr = (StringPtr) nm;
  91.         d.dirInfo.ioVRefNum = cur_vol;
  92.         d.dirInfo.ioFDirIndex = -1;
  93.  
  94.         PBGetCatInfo(&d,0);
  95. /*        if(d.ioResult != noErr) return(0); this is not defined in lightspeed's headers */
  96.         PtoCstr((char *) nm);
  97.         strcpy(tmp,ret);
  98.         if (style == UNIX_FILE_STYLE)
  99.           strcpy(ret,"/");
  100.         else 
  101.           strcpy(ret,":");
  102.         strcat(ret,nm);
  103.         strcat(ret,tmp);
  104.         if(d.dirInfo.ioDrDirID == 2) break;    /* home directory */
  105.         d.dirInfo.ioDrDirID = d.dirInfo.ioDrParID;
  106.     } 
  107.     /* if its MAC style, remove the leading colon */
  108.     if (style == MAC_FILE_STYLE)
  109.       return(ret+1);
  110.     else
  111.       return(ret);
  112. }
  113.  
  114. /*----------------------------------------------------------------------*/
  115. /* Gets a filename from the user (one that exists already) */
  116.  
  117. static boolean 
  118. get_filename(char* prompt, char *filename)
  119. {
  120.    SFReply    macSFReply;
  121.    Point pos;
  122.    pos.h = pos.v = 100;
  123.    SFGetFile(pos, (StringPtr)"\pFile to Index:", 
  124.              NULL, -1, NULL, NULL, &macSFReply);
  125.    if(macSFReply.good)
  126.     { /* then we have a gotten a good file.
  127.        * put together the full filename
  128.        */
  129.          
  130.       strcpy(filename, getwd(MAC_FILE_STYLE));
  131.       strcat(filename, ":");
  132.       PtoCstr((char *)macSFReply.fName);
  133.       strcat(filename, (char *)macSFReply.fName);
  134.       CtoPstr((char *)macSFReply.fName);
  135.       return(true);
  136.      }
  137.     else
  138.       return(false);
  139. }
  140.  
  141. /*----------------------------------------------------------------------*/
  142. /* Gets a filename from the user (one that does not exist already) */
  143.  
  144. static int get_new_filename(char *filename)
  145. {
  146.     SFReply    macSFReply;
  147.     Point pos;
  148.     pos.h = pos.v = 100;
  149.     
  150.     SFPutFile(pos, (StringPtr)"\pLocation of Index:", 
  151.                    (StringPtr)"\pindex", 
  152.               NULL, &macSFReply);
  153.     if(macSFReply.good){
  154.         /* then we have a gotten a good file.
  155.          * put together the full filename
  156.          */
  157.          
  158.          strcpy(filename, getwd(MAC_FILE_STYLE));
  159.          strcat(filename, ":");
  160.          PtoCstr((char *)macSFReply.fName);
  161.          strcat(filename, (char *)macSFReply.fName);
  162.          CtoPstr((char *)macSFReply.fName);
  163.          return(true);
  164.     }
  165.     else{
  166.     return(false);
  167.     }
  168. }
  169.  
  170. /*----------------------------------------------------------------------*/
  171. /* Main routine for building an index */
  172.  
  173. FILE *logfile = NULL;
  174.  
  175. void main()
  176. {    
  177.   char filename[MAX_FILE_NAME_LEN + 1]; 
  178.   char index_filename[MAX_FILE_NAME_LEN + 1];
  179.   database* db;
  180.   long count;
  181.   SFReply macSFReply;
  182.   Point pos;
  183.  
  184.   InitProfile(3000,200);
  185.     
  186. _profile = false;
  187. _trace = false;
  188. freopen("stdout","w",stdout);
  189.     
  190.   pos.h = pos.v = 100;
  191.  
  192.   gApplication = new(CBuildApp);
  193.   ((CBuildApp *)gApplication)->IBuildApp();
  194.   
  195.   if(true == get_new_filename(index_filename))
  196.     printf("The full index filename is: %s\n", index_filename);
  197.   else 
  198.     return;
  199.     
  200.   printf("Initializing Database.\n");
  201.   db = openDatabase(index_filename, true,false);
  202.   if (db == NULL)
  203.     panic("unable to open the database\n");
  204.   db->the_word_memory_hashtable =
  205.     init_word_memory_hashtable(65536L, 500000L, db->the_word_memory_hashtable);
  206.  
  207.       SFGetFile(pos, (StringPtr)"\pFiles to Index:", 
  208.               NULL, -1, NULL, NULL, &macSFReply);
  209.     if(macSFReply.good)
  210.     {
  211.         /* then we have a file.  In this version we index all the
  212.          * files in the volume of the selected file 
  213.          */
  214.          OSErr Error;
  215.          CInfoPBRec PBRec;
  216.          CInfoPBPtr PBPtr = &PBRec;
  217.          
  218.          char nm[50];
  219.          char dirname[500];
  220.  
  221. _profile = true;
  222.  
  223.          strcpy(dirname, getwd(MAC_FILE_STYLE));
  224.          
  225.          /* set up the PBRec */
  226.          PBPtr->hFileInfo.ioVRefNum  = 0 - SFSaveDisk;
  227.          PBPtr->hFileInfo.ioDirID = CurDirStore;
  228.          PBPtr->hFileInfo.ioNamePtr = macSFReply.fName;
  229.          PBPtr->hFileInfo.ioCompletion = NULL;
  230.          PBPtr->hFileInfo.ioFDirIndex = 0; 
  231.          Error = PBGetCatInfo(PBPtr, FALSE); 
  232.          
  233.          PBPtr->hFileInfo.ioVRefNum  = 0 - SFSaveDisk;
  234.          PBPtr->hFileInfo.ioDirID = CurDirStore;
  235.          PBPtr->hFileInfo.ioFDirIndex = 1; 
  236.          PBPtr->hFileInfo.ioNamePtr = (StringPtr) nm;
  237.          while((Error = PBGetCatInfo(PBPtr, FALSE)) != fnfErr)
  238.          {
  239.              /* loop until we are done with the volume */
  240.              PBPtr->hFileInfo.ioVRefNum  = 0 - SFSaveDisk;
  241.              PBPtr->hFileInfo.ioDirID = CurDirStore;
  242.              PBPtr->hFileInfo.ioNamePtr = (StringPtr) nm;
  243.              PBPtr->hFileInfo.ioCompletion = NULL;
  244.             PBPtr->hFileInfo.ioFDirIndex++;
  245.             /* to determine if a record is a directory do
  246.              * PBPtr->hFileInfo.ioFlAttrib & 0x10
  247.              */
  248.             
  249.             strcpy(filename, dirname);
  250.             strcat(filename, ":");
  251.             PtoCstr(nm);
  252.             strcat(filename, nm);
  253.             CtoPstr(nm);
  254.             if(!(PBPtr->hFileInfo.ioFlAttrib & 0x10))
  255.             {
  256.                 printf("Indexing File: %s...\n", filename);
  257.                 index_text_file(filename, 
  258.                         NULL, NULL, NULL, 
  259.                         "TEXT", /* this should be the mac filetype XXX */
  260.                         db,true, false);
  261.             }
  262.             else
  263.                 printf("Directory %s is not indexed\n", filename);
  264.          }
  265.          
  266. _profile = false;
  267.  
  268.     }
  269.  
  270.     printf("Finished entering Files to be indexed.\n");
  271.     printf("Now updating disk files\n");
  272.      finished_add_word(db);
  273.       closeDatabase(db);
  274.     
  275.     printf("Finished with the indexing. Exiting.\n");
  276.     for(count = 0; count < 100000; count++)
  277.       ;  /* delay to show message */
  278.       
  279.     gApplication->Exit();
  280. }
  281.  
  282. /*----------------------------------------------------------------------*/
  283.